<!--  -->
<template>
  <div class="box flex grow column">
    <div class="head flex shrink">
      <div class="type clear">
        <span class="item fl" :class="{ cur: type == '' }" @click="draw1"
          >两融余额</span
        >
        <span
          class="item fl"
          :class="{ cur: type == 's2n' }"
          @click="indexData('s2n')"
          >北向资金</span
        >
        <span
          class="item fl"
          :class="{ cur: type == 'n2s' }"
          @click="indexData('n2s')"
          >南向资金</span
        >
      </div>
    </div>
    <div id="chartbox" class="grow"></div>
  </div>
</template>

<script>
import { getChartList, getIndexData } from "../api/index";
import takeRight from "lodash/takeRight";
import dataHandler from "../script/dataHandler";
import { numberFormat } from "../script/utils";
import * as Highcharts from "highcharts";
import * as Exporting from "highcharts/modules/exporting";
import draw from "../script/draw";

Exporting(Highcharts);

export default {
  data() {
    return {
      chartData1: [],
      chartData2: [],
      type: "s2n", //s2n 南向 n2s 北向
      list: {
        s2n: {
          name: "北向资金",
          dir: "n",
          child: {
            hk2sh: { name: "沪股通", direction: 0, data: [] },
            hk2sz: { name: "深股通", direction: 0, data: [] },
          },
          change: [],
          trend: [],
        },
        n2s: {
          name: "南向资金",
          dir: "s",
          child: {
            sh2hk: { name: "港股通(沪)", direction: 1, data: [] },
            sz2hk: { name: "港股通(深)", direction: 1, data: [] },
          },
          change: [],
          trend: [],
        },
      },
    };
  },

  components: {},

  computed: {},

  async mounted() {
    const data1 = await getChartList();
    this.chartData1 = data1.data.reverse();
    const data2 = await getIndexData();
    this.chartData2 = data2.data;
    this.draw1();
  },

  methods: {
    draw1() {
      this.type = "";
      //显示刻度的位置:
      // 3,5,10日间隔9天显示刻度
      // 1日间隔39天
      const tickPositioner = Array.from({ length: 3 }, (_, i) => 239 - 115 * i);

      const chartData = this.chartData1;
      const datas = dataHandler(chartData);
      Highcharts.chart("chartbox", {
        chart: {
          marginTop: 50,
          //   spacing: [30, 30, 30, 30],
        },
        title: { text: "" },
        credits: { enabled: false },
        exporting: { enabled: false },
        legend: {
          symbolWidth: 5,
          symbolHeight: 5,
          align: "left",
          verticalAlign: "top",
          x: 30,
          y: 0,
          floating: true,
          labelFormatter() {
            return `<span style="color:${this.color}">${this.name}</span>`;
          },
        },
        xAxis: {
          tickPositioner: () => tickPositioner,
          //   showFirstLabel: true,
          //   showLastLabel: true,
          //   tickAmount: 3,
          labels: {
            align: "center",
            formatter() {
              return chartData[this.value].dim_date;
            },
          },
        },
        yAxis: [
          { title: "" },
          {
            title: "",
            opposite: true,
            labels: {
              align: "center",
              formatter() {
                return numberFormat(this.value);
              },
            },
          },
        ],
        series: datas,
        tooltip: {
          //   backgroundColor: "rgba(0, 0, 0, 0.7)",
          //   borderColor: "#666666",
          shared: true,
          formatter: function () {
            // 比较两个点的y轴坐标点的像素值
            const getPx = (point) => point.series.yAxis.toPixels(point.y);
            const isGreat = getPx(this.points[0]) - getPx(this.points[1]) >= 0;
            return this.points.reduce(function (prev, point) {
              // 如果@开头使用计算
              const name = point.series.name;

              const a = `${prev}<br/>`;
              const b = `<span style="font-size:14px;color:${
                point.color
              }">${name}: ${numberFormat(point.y)}</span><br/>`;

              return isGreat ? b + a : a + b;
            }, `${chartData[this.x].dim_date}`);
          },
        },
        plotOptions: {
          column: {
            borderWidth: 0,
          },
          spline: {
            lineWidth: 1,
            marker: {
              enabled: false,
              symbol: "circle",
            },
            states: {
              hover: {
                lineWidth: 1,
              },
            },
          },
        },
      });
    },
    async indexData(type) {
      if (!this.chartData2) return;
      this.type = type;
      let cur = this.list[this.type];
      let change = this.chartData2.change.filter(
        (item) => this.type && cur.child[item.type]
      );
      let stock = this.chartData2.stock.filter(
        (item) => this.type && cur.child[item.type]
      );
      let trend = this.chartData2.trend.filter((item) => this.type && cur);
      cur.change = change
        .map((item) => {
          item.color1 = {
            green: item.dayNetAmtIn < 0,
            red: item.dayNetAmtIn > 0,
          };
          item.color2 = {
            green: item.blockChange < 0,
            red: item.blockChange > 0,
          };
          item.dayNetAmtIn = numberFormat(item.dayNetAmtIn * 10000);
          item.dayAmtRemain = numberFormat(item.dayAmtRemain * 10000);
          item.blockChange = item.blockChange / 100 + "%";
          item.uPercent = (item.up / (item.down + item.chp + item.up)) * 100;
          item.dPercent = (item.down / (item.down + item.chp + item.up)) * 100;
          item.cPercent = (item.chp / (item.down + item.chp + item.up)) * 100;
          return item;
        })
        .sort((a, b) => a.id - b.id);
      stock.forEach((item) => {
        item.color = {
          red: item.px_change_rate > 0,
          green: item.px_change_rate < 0,
        };
        item.px_change_rate = item.px_change_rate + "%";
        item.business_amount = numberFormat(item.business_amount);
        cur.child[item.type].data.push(item);
      });
      trend.forEach((item) => {
        this.list[item.type].trend.push(item);
      });
      const dir = this.list[type].dir;
      const data = this.list[type].trend;
      draw({
        container: "chartbox",
        data,
        type: "balance",
        dir,
      });
    },
  },
};
Highcharts.theme = {
  chart: {
    backgroundColor: "#090910",
    plotBorderColor: "#606063",
  },
  xAxis: {
    gridLineColor: "#2C2C30",
    labels: {
      style: {
        color: "#E0E0E3",
      },
    },
    lineColor: "#2C2C30",
    minorGridLineColor: "#505053",
    tickColor: "#2C2C30",
  },
  yAxis: {
    gridLineColor: "#2C2C30",
    labels: {
      style: {
        color: "#E0E0E3",
      },
    },
    lineColor: "#2C2C30",
    minorGridLineColor: "#505053",
    tickColor: "#2C2C30",
    tickWidth: 1,
  },
  tooltip: {
    backgroundColor: "#2A2D40",
    borderColor: "#414663",
    style: {
      color: "#FFFFFF",
    },
  },
  plotOptions: {
    series: {
      dataLabels: {
        color: "#F0F0F3",
        style: {
          fontSize: "13px",
        },
      },
      marker: {
        lineColor: "#333",
      },
    },
    boxplot: {
      fillColor: "#505053",
    },
  },
  labels: {
    style: {
      color: "#2C2C30",
    },
  },
};

Highcharts.setOptions(Highcharts.theme);
</script>
<style lang="scss" scoped>
@import "../style/var.scss";
.box {
  background: $bg;
  height: 100%;
  border: 1px solid #000;
  .head {
    background: $border;
    align-items: center;
    height: 34px;
    text-align: center;
    position: relative;
    .type {
      .item {
        height: 24px;
        line-height: 24px;
        color: #ffffff;
        font-size: 16px;
        padding: 0 8px;
        cursor: pointer;
        position: relative;
        margin-left: 10px;
        &.cur {
          background: #35497c;
          border-radius: 4px;
        }
      }
    }
  }
}
</style>
